<template>
	<div class="page-wrap">
		<div class="page-content p-0">
			<div class="page-column p-0">
				<div class="page-form p-x-0">
					<el-form
						:model="ruleForm"
						:rules="rules"
						label-position="left"
						label-width="140px"
						ref="form"
						label-suffix="："
					>
						<el-row>
							<el-col :span="24">
								<el-table ref="multipleTable" :data="ruleForm.CONSUMPTtab" tooltip-effect="dark" border>
									<el-table-column prop="CONSUMPT_NAM" label="农作物" align="center" min-width="120"></el-table-column>
									<el-table-column prop="CONSUMPT_RAT" label="食物消耗率" align="center" min-width="120">
										<template slot-scope="scope">
											<el-form-item
												label-width="0px"
												:rules="rules.CONSUMPT_RAT"
												:prop="'CONSUMPTtab.'+scope.$index+'.CONSUMPT_RAT'"
											>
												<el-input
													minlength="1"
													maxlength="20"
													oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,20})?).*$/g, '$1')"
													class="bigForm"
													v-model="scope.row.CONSUMPT_RAT"
												></el-input>
											</el-form-item>
										</template>
									</el-table-column>
									<el-table-column prop="AGPRODUC_RAT" label="农作物生产率" align="center" min-width="120">
										<template slot-scope="scope">
											<el-form-item
												label-width="0px"
												:rules="rules.AGPRODUC_RAT"
												:prop="'CONSUMPTtab.'+scope.$index+'.AGPRODUC_RAT'"
											>
												<el-input
													minlength="1"
													maxlength="20"
													oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,20})?).*$/g, '$1')"
													class="bigForm"
													v-model="scope.row.AGPRODUC_RAT"
												></el-input>
											</el-form-item>
										</template>
									</el-table-column>
									<el-table-column prop="PROCLOSS_RAT" label="食入农作物系数" align="center" min-width="120">
										<template slot-scope="scope">
											<el-form-item
												label-width="0px"
												:rules="rules.PROCLOSS_RAT"
												:prop="'CONSUMPTtab.'+scope.$index+'.PROCLOSS_RAT'"
											>
												<el-input
													minlength="1"
													maxlength="20"
													oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,20})?).*$/g, '$1')"
													class="bigForm"
													v-model="scope.row.PROCLOSS_RAT"
												></el-input>
											</el-form-item>
										</template>
									</el-table-column>
									<el-table-column prop="HOLDUPTM_RAT" label="农作物消耗时间" align="center" min-width="120">
										<template slot-scope="scope" v-if="scope.$index<5">
											<el-form-item
												label-width="0px"
												:rules="rules.HOLDUPTM_RAT"
												:prop="'CONSUMPTtab.'+scope.$index+'.HOLDUPTM_RAT'"
											>
												<el-input
													minlength="1"
													maxlength="20"
													oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,20})?).*$/g, '$1')"
													class="bigForm"
													v-model="scope.row.HOLDUPTM_RAT"
												></el-input>
											</el-form-item>
										</template>
									</el-table-column>
								</el-table>
							</el-col>
						</el-row>
					</el-form>
				</div>
				<div class="bottom-btn">
					<el-button type="primary" :loading="loading" @click="submitFun">提 交</el-button>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
import selectPara from "@/components/selectPara"
import commCheckbox from "@/components/commCheckbox"
import ImageCropper from "@/components/ImageCropper"
import FixBottomButton from "@/components/FixBottomButton"
import ueditor from "@/components/ueditor"
import textLength from "@/components/common/comp/textLength"
import { commPara } from "@/mixins/mixins"
import { mapState } from "vuex"
import { dome } from "@/utils/verify.js"
export default {
	name: "aboutUs",
	props: {},
	mixins: [commPara],
	components: {
		selectPara,
		ImageCropper,
		textLength,
		commCheckbox,
		FixBottomButton,
		ueditor,
	},

	data() {
		return {
			activeTab: "3",
			ruleForm: {
				SRCRIORG001: "",
			},
			rules: {
				NUMDATES001: [
					{
						required: true,
						message: "请选择",
						trigger: "change",
					},
				],
				CONSUMPT_RAT: [
					{
						validator: dome(0, 1000.0),
						trigger: ["change", "blur"],
					},
				],
				AGPRODUC_RAT: [
					{
						validator: dome(0, 1000.0),
						trigger: ["change", "blur"],
					},
				],
				PROCLOSS_RAT: [
					{
						validator: dome(0, 10.0),
						trigger: ["change", "blur"],
					},
				],
				HOLDUPTM_RAT: [
					{
						validator: dome(0, 60.0),
						trigger: ["change", "blur"],
					},
				],
			},
			NUMDATES001options: [],
			ACCDATES001tab: [],
			loading: false,
		}
	},
	created() {
		// this.queryDetail()
	},
	mounted() {
		this.getoption()
	},
	computed: {
		...mapState({
			ingestion_Data: (state) => state.token.ingestion_Data,
		}),
	},
	methods: {
		getoption() {
			// console.log(this.ingestion_Data)
			let params = {
				dictypeid: 3,
			}
			// console.log(params)
			this.loading = true
			this.$appApi
				.ZD_GET_DICINFOS(params)
				.then((res) => {
					var str = res.data.substring(0, 1)
					if (str == "E") {
						let message = res.data.substring(6)
						this.$notify.error({
							title: "请求数据失败！",
							message: message,
							type: "success",
						})
					} else if (str == "S") {
						let tempCONSUMPT_RAT = "15.0"
						let tempAGPRODUC_RAT = "15.0E-4"
						let tempPROCLOSS_RAT = "0.8"
						this.ruleForm.CONSUMPTtab = JSON.parse(
							res.data.substring(8)
						).map((item, index) => {
							return {
								CONSUMPT_NAM: item.dic_cname,
								CONSUMPT_NAME: item.dic_ename,
								CONSUMPT_RAT: this.ingestion_Data.CONSUMPT_RAT[
									index
								]
									? this.ingestion_Data.CONSUMPT_RAT[index].toString()
									: "0",
								AGPRODUC_RAT: this.ingestion_Data.AGPRODUC_RAT[
									index
								]
									? this.ingestion_Data.AGPRODUC_RAT[index].toString()
									: "0",
								PROCLOSS_RAT: this.ingestion_Data.PROCLOSS_RAT[
									index
								]
									? this.ingestion_Data.PROCLOSS_RAT[index].toString()
									: "",
								HOLDUPTM_RAT: this.ingestion_Data.HOLDUPTM_RAT[
									index
								]
									? this.ingestion_Data.HOLDUPTM_RAT[index].toString()
									: "0",
							}
						})
						this.loading = false
					}
				})
				.catch((err) => {
					console.log(err)
				})
		},
		submitFun() {
			//保存
			this.$refs.form.validate((valid) => {
				if (valid) {
					// console.log(this.$refs.multipleTable)
					// console.log(this.ingestion_Data)
					let tempdata = JSON.parse(
						JSON.stringify(this.ingestion_Data)
					)
					tempdata.CONSUMPT_NAM = this.ruleForm.CONSUMPTtab.map(
						(item) => {
							return item.CONSUMPT_NAME
						}
					)
					tempdata.CONSUMPT_RAT = this.ruleForm.CONSUMPTtab.map(
						(item) => {
							return parseFloat(item.CONSUMPT_RAT)
						}
					)
					tempdata.AGPRODUC_RAT = this.ruleForm.CONSUMPTtab.map(
						(item) => {
							return parseFloat(item.AGPRODUC_RAT)
						}
					)
					tempdata.PROCLOSS_RAT = this.ruleForm.CONSUMPTtab.map(
						(item) => {
							return parseFloat(item.PROCLOSS_RAT)
						}
					)
					let tempHOLDUPTM_RAT = this.ruleForm.CONSUMPTtab.map(
						(item) => {
							return parseFloat(item.HOLDUPTM_RAT)
						}
					)

					tempdata.HOLDUPTM_RAT = tempHOLDUPTM_RAT.splice(0, 5)
					tempdata.SETPAGEINDEX = this.activeTab
					this.$store.dispatch("upData_ingestion_Data", tempdata)
					// console.log(this.ingestion_Data)
					let params = {
						batchid: this.$route.query.id,
						batchinfo: "SHIRU",
						submitjson: JSON.stringify(tempdata),
					}
					// console.log(params)
					this.$appApi
						.BATCH_SUBMIT(params)
						.then((res) => {
							var str = res.data.substring(0, 1)
							if (str == "E") {
								let message = res.data.substring(6)
								this.$notify.error({
									title: "提交失败！",
									message: message,
									type: "success",
								})
							} else if (str == "S") {
								this.$notify({
									message: "提交成功！",
									type: "success",
								})
								this.$router.push({
									path: "/home",
								})
							}
						})
						.catch((err) => {
							console.log(err)
						})

				}
			})
		},
	},
}
</script>
<style lang="scss" scoped>
.page-flex-row /deep/ .el-collapse-item__header {
	background: #f3f9fe;
}
</style>